package org.realogic.btp.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.realogic.cfm.domain.KQueryParamters;
import org.realogic.cfm.util.UtilString;


public class QueryParamtersFactory {

	private static final String PAGE_SIZE = "pageSize";

	private static final String PAGE_NUMBER = "pageNumber";

	private static final String PREPEND = "prepend";

	private static final String QRY_NAME_FLAG = "qryName";

	private static final String QRY_STATIC_SQL = "qryStaticSql";

	private static final String QRY_PARAM_NAMES_FLAG = "qryParamNames";

	private static final String QRY_LOGIC_FIELDS_FLAG = "qryLogicFields";

	private static final String QRY_FIELD_TYPES_FLAG = "qryFieldTypes";

	private static final String QRY_OPERATORS_FLAG = "qryOperators";

	private static final String QRY_LOGIC_Rule_FLAG = "qryLogicRule";

	private static final String QRY_SORT_FIELDS_FLAG = "qrySortFields";

	private static final String QRY_SORT_TYPE_FLAG = "qrySortType";

	private QueryParamtersFactory() {

	}

	public static KQueryParamters createQueryCondition(HttpServletRequest request) {
		KQueryParamters tmpParams = new KQueryParamters();

		String qryName = request.getParameter(QRY_NAME_FLAG);
		if (StringUtils.isNotBlank(qryName)) {
			tmpParams.setQueryName(qryName);
		}

		// 前缀符号(where and or)
		String prepend = request.getParameter(PREPEND);
		if (StringUtils.isNotBlank(prepend)) {
			tmpParams.add(PREPEND, prepend);
		}

		// 指定无参数SQL 条件
		String staticSql = request.getParameter(QRY_STATIC_SQL);
		if (StringUtils.isNotBlank(staticSql)) {
			tmpParams.add(QRY_STATIC_SQL, staticSql.trim());
		}

		// 参数名称
		String tmpParamNames = request.getParameter(QRY_PARAM_NAMES_FLAG);
		if (StringUtils.isNotBlank(tmpParamNames)) {
			tmpParams.add(QRY_PARAM_NAMES_FLAG, tmpParamNames.trim());
		}

		// 逻辑字段
		String tmpLogicFields = request.getParameter(QRY_LOGIC_FIELDS_FLAG);
		if (StringUtils.isNotBlank(tmpLogicFields)) {
			tmpParams.add(QRY_LOGIC_FIELDS_FLAG, tmpLogicFields.trim());
		}

		// 类型
		String tmpFieldTypes = request.getParameter(QRY_FIELD_TYPES_FLAG);
		if (StringUtils.isNotBlank(tmpFieldTypes)) {
			tmpParams.add(QRY_FIELD_TYPES_FLAG, tmpFieldTypes.trim());
		}

		// 操作
		String tmpOperator = request.getParameter(QRY_OPERATORS_FLAG);
		if (StringUtils.isNotBlank(tmpOperator)) {
			tmpParams.add(QRY_OPERATORS_FLAG, tmpOperator.trim());
		}

		// 条件规则
		String tmpQryLogicRule = request.getParameter(QRY_LOGIC_Rule_FLAG);
		if (StringUtils.isNotBlank(tmpQryLogicRule)) {
			tmpParams.add(QRY_LOGIC_Rule_FLAG, tmpQryLogicRule.trim());
		}

		// 排列的字段
		String sSortFields = request.getParameter(QRY_SORT_FIELDS_FLAG);
		if (StringUtils.isNotBlank(sSortFields)) {
			tmpParams.add(QRY_SORT_FIELDS_FLAG, sSortFields.trim());
		}

		// 排列方式升或降
		String sSortType = request.getParameter(QRY_SORT_TYPE_FLAG);
		if (StringUtils.isNotBlank(sSortType)) {
			tmpParams.add(QRY_SORT_TYPE_FLAG, sSortType.trim());
		}

		// 换页请求
		String sPageNumber = request.getParameter(PAGE_NUMBER);
		String sPageSize = request.getParameter(PAGE_SIZE);
		if (StringUtils.isNotBlank(sPageNumber)) {
			tmpParams.setPageNumber(Integer.parseInt(sPageNumber));
		}
		if (StringUtils.isNotBlank(sPageSize)) {
			tmpParams.setPageSize(Integer.parseInt(sPageSize));
		}

		// 获取-输入条件的参数值
		if (StringUtils.isNotBlank(tmpParamNames)) {
			String[] paramNames = UtilString.tokenize(tmpParamNames, "|");
			String tmpValue = null;
			for (int k = 0; k < paramNames.length; k++) {
				tmpValue =request.getParameter(paramNames[k]);
				if (StringUtils.isNotBlank(tmpValue)) {
					tmpParams.add(paramNames[k], tmpValue.trim());
				}
				
			}
		}
		return tmpParams;
	}

}
